﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

namespace OrderExer
{
    //订单
    [Serializable]
    public class Order
    {
        private int orderNo;//订单号
        private string cname;//商品名称
        private string customername;//客户
        private int amount;//订单金额
        private int numbers;//订单有多少个订单明细
        private List<OrderDetails> orderDetails = new List<OrderDetails>();

       
        public Order() { }
        public Order(int orderNo, string customername, string cname, int amount, int numbers)
        {
            this.orderNo = orderNo;
            this.Cname = cname;
            this.Customername = customername;
            this.Amount = amount;
            this.Numbers = numbers;
            //getOrderDetails();
        }

        public int OrderNo { get { return orderNo; } set { orderNo = value; } }

        public string Cname { get => cname; set => cname = value; }
        public int Amount { get => amount; set => amount = value; }
        public string Customername { get => customername; set => customername = value; }
        public int Numbers { get => numbers; set => numbers = value; }
        internal List<OrderDetails> OrderDetails { get => orderDetails; set => orderDetails = value; }

        public override bool Equals(object obj)
        {
            return obj is Order order &&
                   orderNo == order.orderNo &&
                   Cname == order.Cname &&
                   Customername == order.Customername &&
                   Amount == order.Amount &&
                   Numbers == order.Numbers &&
                   OrderNo == order.OrderNo;
        }
        /*
        public override int GetHashCode()
        {
            return HashCode.Combine(orderNo, Cname, Customername, Amount, Numbers, OrderDetails, OrderNo);
        }
        */
        private void getOrderDetails()
        {
            for (int i = 1; i <= Numbers; i++)
            {
                Console.WriteLine("请输入第" + i + "个订单明细的客户年龄");
                string cusAge = Console.ReadLine();
                Console.WriteLine("请输入第" + i + "个订单明细的商品生产日期");
                string date = Console.ReadLine();
                OrderDetails orderDetail = new OrderDetails(Customername, cusAge, Cname, date);
                bool flag = false;

                foreach (OrderDetails details in OrderDetails)
                {
                    if (orderDetail.Equals(details))
                    {
                        flag = true;
                        Console.WriteLine("该订单明细已存在，无法添加");
                        this.Numbers--;
                        break;
                    }
                }
                if (!flag)
                {
                    OrderDetails.Add(orderDetail);
                }
            }
        }

        public override string ToString()
        {
            string s = "订单号:" + orderNo + ",商品名称" + cname + ",客户名称" + customername + ",订单金额" + amount + ",有" + numbers + "个订单明细";
            foreach (OrderDetails details in OrderDetails)
            {
                s += details.ToString();
            }
            return s;
        }
    }
}
